#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>

#define FILE_NAME "Log.txt"

//每一个宏，对应的数值，只有一个比特位是1，彼此位置不重叠
//#define ONE (1<<0)
//#define TWO (1<<1)
//#define THREE (1<<2)
//#define FOUR (1<<3)
//
//void show(int flags)
//{
//    if(flags & ONE)printf("one\n");
//    if(flags & TWO)printf("two\n");
//    if(flags & THREE)printf("three\n");
//    if(flags & FOUR)printf("four\n");
//}

int main()
{
    //show(ONE);
    //printf("---------------\n");
    //show(TWO);
    //printf("---------------\n");
    //show(ONE | TWO);
    //printf("---------------\n");
    //show(ONE | TWO | THREE);
    //printf("---------------\n");
    //show(ONE | TWO | THREE | FOUR);
    //printf("---------------\n");


    //r(读)、w(写)、r+、w+(读写，不存在创建)、a(追加写入)、a+(覆盖写入)
    //FILE *fp=fopen(FILE_NAME,"r");
    //if(NULL==fp)
    //{
    //    perror("fopen");
    //    return 1;
    //}
   
    //char buffer[64];
    //while(fgets(buffer,sizeof(buffer)-1,fp)!=NULL)
    //{
    //    buffer[strlen(buffer)-1]=0;
    //    puts(buffer);
    //}

    //int cnt=5;
    //while(cnt)
    //{
    //    fprintf(fp,"%s:%d\n","hello world",cnt--);
    //}

    //fclose(fp);

    
    umask(0);
    //O_WRONLY:写入   O_CREAT:文件不存在时创建文件  O_TRUNC:文件里有内容时清空里面的数据  O_APPEND:追加写入
    int fd=open(FILE_NAME,O_WRONLY | O_CREAT | O_TRUNC,0666);
    if(fd<0)
    {
        perror("open");
        return 1;
    }
    int cnt=5;
    char outBuffer[64];
    while(cnt)
    {
    sprintf(outBuffer,"%s:%d\n","HelloWorld",cnt--);
    write(fd,outBuffer,strlen(outBuffer));
    }
    close(fd);
    return 0;
}
